package studystat;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("serial")
public class RegisterController extends HttpServlet {

	
	// deze doPost legt de link met de jsp-files, 
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        String surname = req.getParameter("surname");
        String firstname = req.getParameter("firstname");
        String userID = req.getParameter("userID");
        String[] vakkennaam = req.getParameterValues("vak");
        List<Vak> vakken = doTheCheck(req,vakkennaam);
		Entity student = new Entity("Student");
		student.setProperty("Achternaam", surname);
		student.setProperty("Voornaam", firstname);
		student.setProperty("GoogleAccount",userID);
		//student.setProperty("ISP",vakkencheck);
		resp.sendRedirect("main.jsp");
		register(surname, firstname, userID, vakken);
    }
	
	public void register(String surname, String firstname, String userID, List<Vak> vakken){
		 StudentManager.getInstance().register(firstname, surname, userID, vakken);
	}
	
	public List<Vak> doTheCheck(HttpServletRequest req,String[] vakken){
		List<Vak> isp = new ArrayList<Vak>();
		for(int i = 0 ; i<vakken.length ; i++){
			isp.add(VakManager.getInstance().getVak(vakken[i]));
			}
		return isp;
		}
	
}
